<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8"> 
    <title>history</title>
</head>

<body>

<input type="button" value="35选7" id="input1">
<div id="div1"></div>

<script>

/*onhashchange*/
/*window.onload = function(){
	var oInput = document.getElementById("input1");
	var oDiv = document.getElementById("div1");
	var obj = {};

	oInput.onclick = function(){
		var number = randomNum(35, 7);
		oDiv.innerHTML = number;
		var oRD = Math.random();
		obj[oRD] = number;
		//修改浏览器的hash
		window.location.hash = oRD;

	}

	// onhashchange监听hash
	window.onhashchange = function(){
		var number = obj[window.location.hash.substring(1)] || '';
		oDiv.innerHTML = number;
	}

	function randomNum(alls, count){
		var allArr = [];
		var newArr = [];

		for(var i=1; i<=alls; i++){
			allArr.push(i);
		}
		console.log(allArr);
		for(var i=0; i<count; i++){
			newArr.push(allArr.splice(Math.floor(Math.random()*allArr.length),1));
		}
		return newArr;
	}
}*/


/*html5 history*/
window.onload = function(){
	var oInput = document.getElementById("input1");
	var oDiv = document.getElementById("div1");

	oInput.onclick = function(){
		var number = randomNum(35, 7);
		oDiv.innerHTML = number;
		// 把数据number保存到历史记录中
		history.pushState(number, "");
	}

	window.onpopstate = function(ev){
		var number = ev.state || "";
		oDiv.innerHTML = number;
	}



	function randomNum(alls, count){
		var allArr = [];
		var newArr = [];

		for(var i=1; i<=alls; i++){
			allArr.push(i);
		}
		console.log(allArr);
		for(var i=0; i<count; i++){
			newArr.push(allArr.splice(Math.floor(Math.random()*allArr.length),1));
		}
		return newArr;
	}
}

</script>

</body>
</html>